/**
* @description: 1041. 困于环中的机器人
* @author hewei
* @date 2022/10/7 14:15
* @version 1.0
*/

public class IsRobotBounded {

    public static void main(String[] args) {
        System.out.println(new IsRobotBounded().isRobotBounded("GL"));
    }

    public boolean isRobotBounded(String instructions) {
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < 4; i++) {
            stringBuilder.append(instructions);
        }
        int des = 1;
        int temp1 = 0;
        int temp2 = 0;
        for (int i = 0; i < stringBuilder.length(); i++) {
            char c = stringBuilder.charAt(i);
            if (c != 'G') {
                if (c == 'L') {
                    des -= 1;
                    if (des == 0) des = 4;
                } else {
                    des += 1;
                    if (des == 5) des = 1;
                }
            } else {
                switch (des) {
                    case 1:
                        ++temp1;
                        break;
                    case 2:
                        --temp2;
                        break;
                    case 3:
                        --temp1;
                        break;
                    case 4:
                        ++temp2;
                        break;
                }
            }
        }
        return temp1 == 0 && temp2 == 0;
    }
}
